package com.cloudlinkscm.loms.services.base.notify.domain.notifier;

import com.cloudlinkscm.loms.services.base.notify.dto.SendResultDto;
import com.cloudlinkscm.loms.services.base.notify.model.Notification;
import com.cloudlinkscm.loms.services.base.notify.model.NotificationResult;
import com.cloudlinkscm.loms.services.base.notify.model.entity.Message;

/**
 * @author : tac
 * @date : 2017/6/23
 */
public abstract class AbstractNotifier<MESSAGE extends Message, NOTIFICATION extends Notification, RESULT extends NotificationResult> {
    private Dispatcher<NOTIFICATION, RESULT> dispatcher;
    private RequestResolver<MESSAGE, NOTIFICATION> requestResolver;
    private ResponseResolver<RESULT> responseResolver;

    public AbstractNotifier(
            Dispatcher<NOTIFICATION, RESULT> dispatcher,
            RequestResolver<MESSAGE, NOTIFICATION> requestResolver,
            ResponseResolver<RESULT> responseResolver) {
        this.dispatcher = dispatcher;
        this.requestResolver = requestResolver;
        this.responseResolver = responseResolver;
    }

    public SendResultDto send(MESSAGE message){
        NOTIFICATION notification = requestResolver.doResolve(message);
        RESULT result = dispatcher.dispatch(notification);
        SendResultDto resultDto = responseResolver.doResolve(result);
        return resultDto;
    }
}
